﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class AddUpdateNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Setup CKEditor gọi CKFinder khi thêm Image
        CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = "AdminTools/ckfinder/";
        _FileBrowser.SetupCKEditor(ckeDescription);

        if (!Page.IsPostBack)
        {
            load_RecordStatus();
            load_All_CategoryNews();
            load_CreateDate();

            if (Request.QueryString["id"] != null)
            {
                int iID = int.Parse(Request.QueryString["id"].ToString());
                load_News(iID);
            }
        }
    }

    public void load_RecordStatus()
    {
        ListItem item = new ListItem();

        item.Value = "True";
        item.Text = "Công bố";
        item.Selected = true;
        rdlRecordStatus.Items.Add(item);

        item = new ListItem();
        item.Value = "False";
        item.Text = "Chờ";
        rdlRecordStatus.Items.Add(item);
    }

    public void load_All_CategoryNews()
    {
        ddlCateNews.DataSource = BusLogic.Category.get_FULL_CATEGORY();
        ddlCateNews.DataValueField = "ID";
        ddlCateNews.DataTextField = "NAME";
        ddlCateNews.DataBind();
    }

    public void load_CreateDate()
    {
        txtCreateDate.Text = DateTime.Now.ToShortDateString();
    }

    public void load_News(int iID)
    {
        DataSet dsNews = BusLogic.News.get_NEWS(iID);

        ddlCateNews.SelectedValue = dsNews.Tables[0].Rows[0]["CATEID"].ToString();
        txtTitle.Text = dsNews.Tables[0].Rows[0]["TITLE"].ToString();
        txtSummary.Text = dsNews.Tables[0].Rows[0]["SUMMARY"].ToString();
        if (!String.IsNullOrEmpty(dsNews.Tables[0].Rows[0]["IMAGE"].ToString()))
        {
            imgNews.Src = dsNews.Tables[0].Rows[0]["IMAGE"].ToString();
            hfImage.Value = dsNews.Tables[0].Rows[0]["IMAGE"].ToString();
        }
        else
        {
            imgNews.Src = "~/App_Themes/Images/toolbar/News.jpg";
        }
        ckeDescription.Text = dsNews.Tables[0].Rows[0]["CONTENT"].ToString();
        txtSorted.Text = dsNews.Tables[0].Rows[0]["SORTED"].ToString();
        txtAuthor.Text = dsNews.Tables[0].Rows[0]["AUTHORS"].ToString();
        rdlRecordStatus.SelectedValue = dsNews.Tables[0].Rows[0]["RECORD_STATUS"].ToString();
    }

    protected void lbtSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {                                    
            string strImageNews = hfImage.Value.ToString();

            BusLogic.News.add_NEWS( int.Parse(ddlCateNews.SelectedValue.ToString()),txtTitle.Text, strImageNews, txtSummary.Text, ckeDescription.Text, txtAuthor.Text, DateTime.Parse(txtCreateDate.Text), int.Parse(txtSorted.Text) , Boolean.Parse(rdlRecordStatus.SelectedValue));
        }
        else
        {
            int iID = int.Parse(Request.QueryString["id"].ToString());
            string strImageNews = hfImage.Value.ToString();
            BusLogic.News.update_NEWS(iID, int.Parse(ddlCateNews.SelectedValue.ToString()), txtTitle.Text, strImageNews, txtSummary.Text, ckeDescription.Text, txtAuthor.Text, DateTime.Parse(txtCreateDate.Text), int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));
        }

        Response.Redirect("NewsManager.aspx");
    }
}
